package com.nanhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.common.util.PageResult;
import com.nanhang.entity.student;
import com.nanhang.mapper.studentMapper;
import com.nanhang.service.studentService;
import com.nanhang.vo.StudentVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import java.util.List;

@Service
public class studentServiceImpl  extends ServiceImpl<studentMapper, student> implements studentService {

// 1. 分页查询
    @Override
    public PageResult<student> findByPage(Integer page, Integer pageSize) {
        Page<student> page1 = this.page(new Page<>(page, pageSize));
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    // 2. 条件分页查询
    @Override
    public PageResult<student> search(Integer page, Integer pageSize, StudentVO vo) {
        QueryWrapper<student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid());
        // 开始分页查询
        Page<student> page1 = this.page(new Page<>(page, pageSize), wrapper);

        // 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

}
